import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import dao.JavaDao;
import entity.Java;

import openConn.DruidUtils;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.logging.Level;

public class TestZhiPin {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入地址码");
            String address = scanner.nextLine();
            System.out.println("请输入关键字");
            String job = scanner.nextLine();
            int page = 1;
            readHtmlData(address,job,1);
        }





    }

    public static void readHtmlData(String addressCode,String job,int page) throws IOException, SQLException, ClassNotFoundException {
//        String drive = "com.mysql.jdbc.Driver";
//        Class.forName(drive);
//        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/reptile?useUnicode=true&characterEncoding=UTF-8","root","123456");
        Connection connection = DruidUtils.openConn();
        Document document = getDocument("https://www.zhipin.com/"+addressCode+"/?query="+job+"&page="+page+"&ka=page-"+page);
        Elements elements = document.select("ul>li>.job-primary");
        if(elements.size() == 0){
            System.out.println("数据读取完毕！！！！");
            return;
        }else{
            for(Element element:elements){
                String jobId = element.selectFirst(".job-title>.job-name>a").attr("data-jobid");
                String jobName = element.selectFirst(".job-title>.job-name>a").text();
                String jobCity = element.selectFirst(".job-title>.job-area-wrapper>span").text();
                String company = element.selectFirst(".info-company>.company-text>.name>a").text();
                String salary = element.selectFirst(".job-limit>span").text();
                String experience = element.selectFirst(".job-limit>p").textNodes().get(0).text();
                String ed = element.selectFirst(".job-limit>p").textNodes().get(1).text();
                //String ed = element.selectFirst(".job-limit>p>.vline").text();
               System.out.println(jobId+" "+jobName+" "+jobCity+" "+company+" "+salary+" "+experience+" "+ed);
                Java java = new Java(jobId,jobName,jobCity,company,salary,experience,ed);
                JavaDao.add(java,connection);
            }
            readHtmlData(addressCode,job,++page);

        }
    }

    public static Document getDocument(String url) throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        //屏蔽日志信息
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        // webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(5000);

        System.out.println(url);
        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(5000);
        String html = rootPage.asXml();

        //
        Document document = Jsoup.parse(html);
        return  document;
    }
}
